layui.config({
    base: "../../js/"
}).use(['laydate', 'laypage', 'layer', 'table', 'carousel', 'upload', 'element', "form"], function() {

    var flow = layui.flow,
        form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        upload = layui.upload,
        $ = layui.jquery;
    var laydate = layui.laydate //日期
        ,
        laypage = layui.laypage //分页
    layer = layui.layer //弹层
        , table = layui.table //表格
        , carousel = layui.carousel //轮播
        , upload = layui.upload //上传
        , element = layui.element //元素操作
        , form = layui.form;
    // 设置全局页数
    var allPages = '';
    //监听Tab切换
    element.on('tab(demo)', function(data) {
        layer.msg('切换了：' + this.innerHTML);
        console.log(data);
    });
    console.log("pppppppppppppppppppppppppppppp")
    var tableIns = table.render({
        elem: '#lunboList',
        cellMinWidth: 95,
        page: false,
        height: "full-125",
        contentType: 'application/json; charset=UTF-8',
        // limits: [10, 15, 20, 25],
        // limit: 20,
        id: "userListTable",
        method: 'POST', //方式
        where: {
            page: 1
        },
        even: false,

        cols: [
            [{
                    type: "checkbox",
                    fixed: "left",
                    width: 50
                },
                {
                    field: 'lunbo_id',
                    title: 'ID',
                    minWidth: 90,
                    align: "center"
                },
                {
                    field: 'image',
                    title: '轮播图',
                    minWidth: 100,
                    align: 'center',
                    templet: function(d) {
                        return '<img height="200%" src="' + 'https://www.logclub.com/xahh/1/' + d.image + '"></img>';
                    }
                },
                {
                    field: 'title',
                    title: '轮播图标题',
                    minWidth: 90,
                    align: "center"
                },

                {
                    field: 'create_time',
                    title: '创建时间',
                    align: 'center'
                },
                {
                    field: 'click_counts',
                    title: '点击次数',
                    align: 'center'
                },

                {
                    field: 'status',
                    title: '是否显示',
                    align: 'center',
                    templet: function(d) {
                        return '<input  class="isChecked disabledZhaZha" type="checkbox" name="status" lay-filter="status" lay-skin="switch" lay-text="是|否" ' + (d.status == '1' ? "checked" : "") + '>'
                    }
                },
                {
                    field: 'recruit_id',
                    title: '招聘职位',
                    align: 'center',
                    minWidth: 220,
                    templet: function(d) {
                        console.log("00000")
                        console.log(d.recruit_id.length)
                        return '<p>' + (d.recruit_id.length == 0 ? "该招聘已撤销,请及时删除轮播图信息" : d.recruit_id.title) + '</p>';
                    }
                },
                // { field: 'city', title: '注册时间', align: 'center', minWidth: 150 },

                // { field: 'country', title: '最后登录时间', align: 'center', minWidth: 150 },
                // { title: '操作', templet: '#userListBar', fixed: "right", align: "center" }
                {
                    fixed: 'right',
                    title: "操作",
                    width: 165,
                    align: 'center',
                    toolbar: '#barDemo'
                },

            ]
        ],
        url: severUrl + "get_lunbo_data",
        // url: "../../json/userList.json",

        done: function(res, page, count) {
            console.log("ppppp");
            // console.log(res);
            // console.log(page);
        },
    });
    // console.log("侧分页")
    // console.log(allPages)

    // 分页
    $.ajax({
        type: "POST",
        url: severUrl + 'get_lunbo_data',
        data: {
            page: 1
        },
        dataType: "json",
        success: function(result) {
            console.log(result);
            console.log("pppp")
            laypage.render({
                elem: 'test3',
                count: result.my_count, //数据总数，从服务端得到
                curr: 1,
                jump: function(obj, first) {

                    //obj包含了当前分页的所有参数，比如：
                    // console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
                    // console.log(obj.limit); //得到每页显示的条数

                    //首次不执行
                    if (!first) {
                        console.log("999999999999")
                        console.log(obj);
                        // var index = layer.msg('修改中，请稍候', { icon: 16, time: false, shade: 0.8 });
                        //     setTimeout(function() {
                        //         layer.close(index);
                        //         if (data.elem.checked) {
                        //             layer.msg("显示成功！");
                        //         } else {
                        //             layer.msg("成效显示！");
                        //         }
                        //     }, 500);
                        // tableIns.config.method = 'post';
                        console.log(obj.curr)
                        table.reload("userListTable", {
                                where: {
                                    page: obj.curr
                                }
                            })
                            //do something
                    }
                }
            });
        }

    });

    // 是否显示
    form.on('switch(status)', function(data) {
        console.log("jjjjjjhhhh")
        console.log($("input[name='status']:checkbox:checked").attr("checked", "true").length)
        if ($("input[name='status']:checkbox:checked").attr("checked", "true").length > 5) {
            console.log("轮播图只能显示五张")
            layer.msg("轮播图只能显示五张！");
            tableIns.reload();
        } else {
            var selectIfKey = data.othis;
            // 获取当前所在行                                                                  
            var parentTr = selectIfKey.parents("tr").find("td").eq(1).text(); //轮播图id

            // console.log(parentTr)
            // console.log(data.elem.checked)
            if (data.elem.checked == true) {
                statuStr = 1
            } else {
                statuStr = 0

            }
            var messages = "";
            // var index = layer.msg('修改中，请稍候', {
            //     icon: 16,
            //     time: false,
            //     shade: 0.8
            // });
            setTimeout(function() {
                // layer.close(index);
                $.ajax({
                    type: 'POST',
                    url: severUrl + 'change_lunbo_status', //发送请求  
                    data: {
                        lunbo_id: parentTr,
                        status: statuStr
                    },
                    dataType: "json",
                    success: function(result) {
                        console.log(result)
                        layer.msg(result.data.message);
                        tableIns.reload();
                    }
                })
            }, 500);
        }

    })

    //监听工具条
    table.on('tool(demo)', function(obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
        var data = obj.data //获得当前行数据
            ,
            layEvent = obj.event; //获得 lay-event 对应的值
        if (layEvent === 'del') {
            layer.confirm('真的删除行么', function(index) {
                console.log(data)
                $.ajax({
                    type: "POST",
                    url: severUrl + 'delete_lunbo_data',
                    data: {
                        lunbo_id: data.lunbo_id
                    },
                    dataType: "json",
                    success: function(result) {
                        console.log(result);
                        console.log("pppp")
                    }

                });
                obj.del();

                layer.close(index);

            });
            // obj.del(); //删除对应行（tr）的DOM结构
            //向服务端发送删除指令
        } else if (layEvent === 'edit') {
            layer.msg('编辑操作');
        }
    });
    // ........................................................................

    //流加载图片
    // var imgNums = 15;  //单页显示图片数量
    // flow.load({
    //     elem: '#Images', //流加载容器
    //     done: function(page, next){ //加载下一页
    //         $.get("../../json/images.json",function(res){
    //             //模拟插入
    //             var imgList = [],data = res.data;
    //             var maxPage = imgNums*page < data.length ? imgNums*page : data.length;
    //             setTimeout(function(){
    //                 for(var i=imgNums*(page-1); i<maxPage; i++){
    //                     imgList.push('<li><img layer-src="../../'+ data[i].src +'" src="../../'+ data[i].thumb +'" alt="'+data[i].alt+'"><div class="operate"><div class="check"><input type="checkbox" name="belle" lay-filter="choose" lay-skin="primary" title="'+data[i].alt+'"></div><i class="layui-icon img_del">&#xe640;</i></div></li>');
    //                 }
    //                 next(imgList.join(''), page < (data.length/imgNums));
    //                 form.render();
    //             }, 500);
    //         });
    //     }
    // });

    //设置图片的高度
    // $(window).resize(function(){
    //     $("#Images li img").height($("#Images li img").width());
    // })

    //多图片上传
    // upload.render({
    //     elem: '.uploadNewImg',
    //     url: '../../json/userface.json',
    //     multiple: true,
    //     before: function(obj){
    //         //预读本地文件示例，不支持ie8
    //         obj.preview(function(index, file, result){
    //             $('#Images').prepend('<li><img layer-src="'+ result +'" src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img"><div class="operate"><div class="check"><input type="checkbox" name="belle" lay-filter="choose" lay-skin="primary" title="'+file.name+'"></div><i class="layui-icon img_del">&#xe640;</i></div></li>')
    //             //设置图片的高度
    //             $("#Images li img").height($("#Images li img").width());
    //             form.render("checkbox");
    //         });
    //     },
    //     done: function(res){
    //         //上传完毕
    //     }
    // });

    //弹出层
    // $("body").on("click","#Images img",function(){
    //     parent.showImg();
    // })

    //删除单张图片
    $("body").on("click", ".img_del", function() {
            var _this = $(this);
            layer.confirm('确定删除图片"' + _this.siblings().find("input").attr("title") + '"吗？', {
                icon: 3,
                title: '提示信息'
            }, function(index) {
                _this.parents("li").hide(1000);
                setTimeout(function() {
                    _this.parents("li").remove();
                }, 950);
                layer.close(index);
            });
        })
        // 单个删除图片

    // 全选
    form.on('checkbox(selectAll)', function(data) {
        console.log(data)
        var child = $("#Images li input[type='checkbox']");
        child.each(function(index, item) {
            item.checked = data.elem.checked;
        });
        form.render('checkbox');
    });

    //通过判断是否全部选中来确定全选按钮是否选中
    form.on("checkbox(choose)", function(data) {
            console.log(data)
            var child = $(data.elem).parents('#Images').find('li input[type="checkbox"]');
            var childChecked = $(data.elem).parents('#Images').find('li input[type="checkbox"]:checked');
            if (childChecked.length == child.length) {
                $(data.elem).parents('#Images').siblings("blockquote").find('input#selectAll').get(0).checked = true;
            } else {
                $(data.elem).parents('#Images').siblings("blockquote").find('input#selectAll').get(0).checked = false;
            }
            form.render('checkbox');
        })
        //批量删除
    $(".batchDel").click(function() {
            var checkStatus = table.checkStatus('userListTable'),
                data = checkStatus.data,
                newsId = [];
            console.log(data)
            if (data.length > 0) {
                for (var i in data) {
                    newsId.push(data[i].lunbo_id);
                }
                layer.confirm('确定删除选中的轮播图？', {
                    icon: 3,
                    title: '提示信息'
                }, function(index) {
                    console.log("kkkkewuuty")
                    console.log(newsId)
                    $.post(severUrl + "delete_many_lunbo", {
                        lunbo_ids: JSON.stringify(newsId) //将需要删除的newsId作为参数传入
                    }, function(data) {
                        console.log(data)

                    });
                    tableIns.reload();
                    layer.close(index);
                })
            } else {
                layer.msg("请选择需要删除的用户");
            }
        })
        // 批量删除

    // 添加轮播图
    function addNews(edit) {
        var index = layui.layer.open({
            title: "添加轮播图",
            type: 2,
            content: "addLunBo.html",
            success: function(layero, index) {
                var body = layui.layer.getChildFrame('body', index);
                // if (edit) {
                //     body.find(".newsName").val(edit.newsName);
                //     body.find(".abstract").val(edit.abstract);
                //     body.find(".thumbImg").attr("src", edit.newsImg);
                //     body.find("#news_content").val(edit.content);
                //     body.find(".newsStatus select").val(edit.newsStatus);
                //     body.find(".openness input[name='openness'][title='" + edit.newsLook + "']").prop("checked", "checked");
                //     body.find(".newsTop input[name='newsTop']").prop("checked", edit.newsTop);
                //     form.render();
                // }
                setTimeout(function() {
                    layui.layer.tips('点击此处返回文章列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                }, 500)
            }
        })
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize", function() {
            layui.layer.full(index);
        })
    }
    $(".addLunBo").click(function() {
        addNews();
    })

})